package com.master.demo.im.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.master.common.api.Query;
import com.master.demo.im.pojo.entity.Session;
import com.master.demo.im.pojo.vo.SessionVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 在线会话信息 Mapper 接口
 *
 * @author: Yang
 * @date: 2022/8/8
 * @version: 1.0.0
 * Copyright Ⓒ 2022 恒翔 Computer Corporation Limited All rights reserved.
 */
public interface SessionMapper extends BaseMapper<Session> {

    /**
     * 分页查询在线会话信息列表
     *
     * @param page  分页参数
     * @param query 查询条件
     * @return
     */
    IPage<SessionVO> pageSession(IPage page, @Param("q") Query query);

    /**
     * 查询在线会话信息列表
     *
     * @param query 查询条件
     * @return
     */
    List<SessionVO> listSession(@Param("q") Query query);

    /**
     * 更新用户状态
     *
     * @param uid    用户id
     * @param nid    netty id
     * @param status 0：离线；状态 1：在线
     */
    @Update("update im_session set status = #{status} where uid = #{uid} and nid = #{nid}")
    void updateStatus(@Param("uid") String uid, @Param("nid") String nid, @Param("status") Integer status);

    /**
     * 开启Apns
     *
     * @param uid     用户id
     * @param channel 设备类型
     */
    @Update("update im_session set is_apns = " + Session.STATUS_INACTIVE + " where uid = #{uid} and channel = #{channel}")
    void openApns(@Param("uid") String uid, @Param("channel") String channel);

    /**
     * 关闭Apns
     *
     * @param uid     用户id
     * @param channel 设备类型
     */
    @Update("update im_session set is_apns = " + Session.STATUS_ACTIVE + " where uid = #{uid} and channel = #{channel}")
    void closeApns(@Param("uid") String uid, @Param("channel") String channel);

}
